package com.neu.table;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import com.neu.pojo.Result;
import com.neu.service.IresultService;
import com.neu.service.impl.ResultService;

public class ResultTable extends AbstractTableModel{
	private String[] colNames = {"姓名","性别","模板名称","模板类型","时间","评估人","建议"};
	private Object[][] data;
	//业务
	private IresultService resultService = ResultService.getInstance();
	
	public ResultTable() {
		ArrayList<Result> results = resultService.list();
		
		if(results.size()!=0&&results!=null) {
			this.data = new Object[results.size()][colNames.length];
			
			for(int i=0;i<results.size();i++) {
				data[i][0] = results.get(i).getName();
				data[i][1] = results.get(i).getSex();
				data[i][2] = results.get(i).getModelName();
				data[i][3] = results.get(i).getModelKind();
				data[i][4] = results.get(i).getDate();
				data[i][5] = results.get(i).getWorker();
				data[i][6] = results.get(i).getSuggestion();
			}
		}else {
			this.data = new Object[0][0];
		}
	}
	
	//返回结果行数
	@Override
	public int getRowCount() {
		return this.data.length;
	}

	//返回结果列数
	@Override
	public int getColumnCount() {
		return this.colNames.length;
	}
	
	//获得对应列名
	@Override
	public String getColumnName(int column) {
		return this.colNames[column];
	}

	//每列对应的类型
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return data[0][columnIndex].getClass();
	}
	
	//返回哪行哪列对应的数据
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex];
	}
	
	/**
	 * 哪列不能编辑
	 * 返回false 不可编辑
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
//		if (columnIndex == 1) {		//编号不能编辑
//			return false;
//		} else
			return true;
	}
	
	//修改表格中的数据
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		data[rowIndex][columnIndex] = aValue;
		/*修改表格数据后，显示改后结果*/
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public void fireTableRowsDeleted(int firstRow, int lastRow) {
		super.fireTableRowsDeleted(firstRow, lastRow);
	}
	
}
